Sentinel-2 Soil Moisture Data Aggrigation with Uber H3 Part 2

Sam Ericksen

Resolution = 12

Author: Sam Ericksen

Date: 4/7/2020

Although the indices created from the sentinel-2 data are useful by themselves in many applications, they are still a pixelized raster format. This means that we cannot easily join them with other data. In our case we are using soil moisture as one of many variables to predict good habitat for a local plant. Because we are looking at other variables too, it would be nice if we

First lets load in some packages to help us work with spatial data. (Note: if you are continuing working from Part 1 directly, you only need to load in the h3 package).

library(h3) #aggrigation on Ubers H3 hexagon grid
library(terra) #Spatial data tools
terra 1.5.21
library(leaflet) #Interactive map viewer
library(sf) #for conversion between spatial formats
Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(raster)
Loading required package: sp
library(RColorBrewer)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:raster':

    intersect, select, union
The following objects are masked from 'package:terra':

    intersect, src, union
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Loading in the Data

Note: If you are coming directly from the post “Sentinel-2 Soil Moisture Data Mining Part 1” you will be able to use the cummulative_moisture variable to work with the aggregated raster already loaded into memory, and the next step can be skipped.

A handy method for reading files from your local drive is to use the file.choose() function from base r. This function opens up a file explorer GUI window and allows the user to select files directly from the local drive.

It’s also worth mentioning, that in the case you wanted to grab several files all in the same folder you can use the function dir.choose(), which allows the user to select a folder, in conjunction with list.files() to generate a list of paths to all files in a directory. This means that if you repeated Part 1 for several different date ranges, and then outputted all cumulative rasters to one folder, you could easily read them into a vector and use lapply() to apply most of the functions in this tutorial to all of the rasters.

Here we will demonstrate the use of the file.choose() function, and although the resultant GUI won’t be displayed on this page, when you run it on your local machine you should find the function quite intuitive.

rast_path <- "cummulative_moisture_example.tif" #find and select raster, create char vector of path to raster

webmercProj <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs" # crs from previous exercise
# 
cummulative_moisture <- terra::rast(rast_path) #read in raster from path from previous exercise

Raster to Vector Aggrigation

Create Empty H3 polygons for the AOI

First we need to create some empty polygons (hexagons) to summarize the raster values within. To do this we simply use our previous polygon for the AOI (Part 1). Because we wrote the GeoJSON for the AOI to a file we can simply read it in here.

laguna_file <- "Laguna_Ext.GeoJSON"

laguna_Ext <- read_sf(laguna_file)

leaflet(laguna_Ext) %>% 
  addTiles() %>% 
  addPolygons()

Looks like we’re still in the right place. Next lets construct a grid of H3 hexes for the area, with a resolution of 12 (based on resolution used for other variables).

laguna_h3 <- 
  geo_to_h3(laguna_Ext) %>% #create all encompassing hexagons index list
  h3_to_children(res = 12) %>% #get children of total hex indexes at desired resolution
  h3_to_geo_boundary_sf()

Extract Values

(Mieno, n.d.)

Mieno, Taro. n.d. 5.2 Extracting Values from Raster Layers for Vector Data | r as GIS for Economists. https://tmieno2.github.io/R-as-GIS-for-Economists/extracting-values-from-raster-layers-for-vector-data.html.